package cn.iocoder.yudao.module.bpm.controller.admin.tohandle.sourceconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.bpm.controller.admin.tohandle.sourceconfig.vo.*;
import cn.iocoder.yudao.module.bpm.convert.tohandle.sourceconfig.BpmHandleSourceConfigConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tohandle.BpmHandleSourceConfigDO;
import cn.iocoder.yudao.module.bpm.service.tohandle.sourceconfig.BpmHandleSourceConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 审批流待办中心——来源系统配置")
@RestController
@RequestMapping("/bpm/handle-source-config")
@Validated
public class BpmHandleSourceConfigController {

    @Resource
    private BpmHandleSourceConfigService handleSourceConfigService;

    @PostMapping("/addToHandleFlowTask")
    @Operation(summary = "创建审批流待办中心——测试权限")
    @PreAuthorize("@ss.hasPermission('bpm:handle-center:create')")
    public CommonResult<Long> addToHandleFlowTask(@Valid @RequestBody BpmHandleSourceConfigCreateReqVO createReqVO) {
        return null;
    }

    @PostMapping("/create")
    @Operation(summary = "创建审批流待办中心——来源系统配置")
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:create')")
    public CommonResult<Long> createHandleSourceConfig(@Valid @RequestBody BpmHandleSourceConfigCreateReqVO createReqVO) {
        return success(handleSourceConfigService.createHandleSourceConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审批流待办中心——来源系统配置")
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:update')")
    public CommonResult<Boolean> updateHandleSourceConfig(@Valid @RequestBody BpmHandleSourceConfigUpdateReqVO updateReqVO) {
        handleSourceConfigService.updateHandleSourceConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审批流待办中心——来源系统配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:delete')")
    public CommonResult<Boolean> deleteHandleSourceConfig(@RequestParam("id") Long id) {
        handleSourceConfigService.deleteHandleSourceConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审批流待办中心——来源系统配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:query')")
    public CommonResult<BpmHandleSourceConfigRespVO> getHandleSourceConfig(@RequestParam("id") Long id) {
        BpmHandleSourceConfigDO handleSourceConfig = handleSourceConfigService.getHandleSourceConfig(id);
        return success(BpmHandleSourceConfigConvert.INSTANCE.convert(handleSourceConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得审批流待办中心——来源系统配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:query')")
    public CommonResult<List<BpmHandleSourceConfigRespVO>> getHandleSourceConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<BpmHandleSourceConfigDO> list = handleSourceConfigService.getHandleSourceConfigList(ids);
        return success(BpmHandleSourceConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审批流待办中心——来源系统配置分页")
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:query')")
    public CommonResult<PageResult<BpmHandleSourceConfigRespVO>> getHandleSourceConfigPage(@Valid BpmHandleSourceConfigPageReqVO pageVO) {
        PageResult<BpmHandleSourceConfigDO> pageResult = handleSourceConfigService.getHandleSourceConfigPage(pageVO);
        return success(BpmHandleSourceConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审批流待办中心——来源系统配置 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:handle-source-config:export')")
    @OperateLog(type = EXPORT)
    public void exportHandleSourceConfigExcel(@Valid BpmHandleSourceConfigExportReqVO exportReqVO,
                                              HttpServletResponse response) throws IOException {
        List<BpmHandleSourceConfigDO> list = handleSourceConfigService.getHandleSourceConfigList(exportReqVO);
        // 导出 Excel
        List<BpmHandleSourceConfigExcelVO> datas = BpmHandleSourceConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "审批流待办中心——来源系统配置.xls", "数据", BpmHandleSourceConfigExcelVO.class, datas);
    }

}
